package com.zhongyu.controller;
/**
销售订单
 */
import com.zhongyu.pojo.Order;
import com.zhongyu.pojo.OrderDetail;
import com.zhongyu.pojo.User;
import com.zhongyu.service.OrderDetailService;
import com.zhongyu.service.OrderService;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import com.zhongyu.vo.XsOrderVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
@RestController
@RequestMapping("/sal/xxddlr")
public class XsOrderController {
@Resource
   private OrderService orderService;
    @Resource
    private OrderDetailService orderDetailService;
/*
销售查询
 */
    @GetMapping("/search/{pageNo}/{pageSize}")
    @RequiresPermissions("xxddcx:list")
    public ResultVo search(@PathVariable Integer pageNo, @PathVariable Integer pageSize, XsOrderVo xsOrderVo){
        PageResult pageResult=orderService.searchAll(pageNo,pageSize,xsOrderVo);
        return ResultVo.success(pageResult);

    }

    /**
     * 销售出库
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/search1/{pageNo}/{pageSize}")
    @RequiresPermissions("xxddck:list")
    public ResultVo search1(@PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageResult pageResult=orderService.search1(pageNo,pageSize);
        return ResultVo.success(pageResult);
    }
    /*
    销售录入
     */
    @GetMapping("/search2/{pageNo}/{pageSize}")
    @RequiresPermissions("xxddlr:list")
    public ResultVo search2(@PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageResult pageResult=orderService.search1(pageNo,pageSize);
        return ResultVo.success(pageResult);
    }
  //  @GetMapping("/list")
  //  public ResultVo list(HttpSession  session){
     //   List<Order> list= orderService.searchAll();
    //    return  ResultVo.success(list);
  //  }
  @GetMapping("/list")
   public ResultVo list(Integer id, HttpSession  session){
  //List<Order> list= orderService.searchById(id);
    List<OrderDetail> list= orderDetailService.findById(id);  //findByorderId
  /*    list.forEach(order->{
          List<OrderDetail> detailList=orderDetailService.findById(order.getId());
          order.getDetailList().addAll(detailList);
      });*/
     return  ResultVo.success(list);
  }
    @GetMapping("/list1")
    public ResultVo list1(Integer detailId, HttpSession  session){
        List<OrderDetail> list= orderDetailService.findBydetailId1(detailId);
        return  ResultVo.success(list);
    }
  @PostMapping("/add")
  public ResultVo add(@RequestBody Order order, HttpSession session){
      try{
          if(order.getProviderId()==null){
              return ResultVo.failure("请选择客户");
          }
          if(order.getOrderDetail()==null||order.getOrderDetail().isEmpty()){
              return   ResultVo.failure("商品信息不能为空");
          }
          User loginUser= (User) session.getAttribute("loginUser");
          order.setCreator(loginUser.getId());
          return   orderService.xsadd(order);
      }catch (Exception ex){
          return ResultVo.failure("添加销售订单失败",ex);
      }
  }
}
